package controllers.cell;

import com.jfoenix.controls.JFXButton;
import controllers.SystemViewController;
import controllers.ViewManager;
import database.dao.Impl.CommodityDaoImpl;
import entity.Commodity;
import javafx.event.ActionEvent;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFormatter;
import services.SystemContext;

import java.net.URL;
import java.text.DecimalFormat;
import java.util.ResourceBundle;
import java.util.function.UnaryOperator;

/**
 * @ClassName CommodityCellController
 * @Description TODO
 * @Author lzq
 * @Date 2021/6/18 10:53
 */
public class CommodityCellController implements Initializable {
    public TextField surplus;
    public JFXButton deleteBtn;
    public TextField priceFd;
    public TextArea commitTA;
    public TextField nameFd;
    public TextField numberField;
    public JFXButton addBtn;
    public JFXButton updateBtn;
    public Label id;
    private Boolean Addflag;
    private Boolean updateFlag;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        updateFlag=false;
        Addflag =false;
        surplus.setEditable(false);
        priceFd.setEditable(false);
        commitTA.setEditable(false);
        nameFd.setEditable(false);

        numberField.setTextFormatter(new TextFormatter<String>(new UnaryOperator<TextFormatter.Change>() {
            @Override
            public TextFormatter.Change apply(TextFormatter.Change change) {
                String value = change.getText();
                if(value.matches("[0-9]*")){
                    return change;
                }
                return null;
            }
        }));
    }

    public void deleteCommodity(ActionEvent actionEvent) {
        CommodityDaoImpl commodityDao=new CommodityDaoImpl();
        commodityDao.deleteCommodity(id.getText());
        SystemContext.getInstance().refreshCommodityList();
    }

    public void UpdateCommodity(ActionEvent actionEvent) {
        if(updateFlag){
            updateFlag=false;
            surplus.setEditable(false);
            priceFd.setEditable(false);
            commitTA.setEditable(false);
            nameFd.setEditable(false);
            updateBtn.setText("更改信息");
            StorageChange();
        }else {
            updateFlag=true;
            surplus.setEditable(true);
            priceFd.setEditable(true);
            commitTA.setEditable(true);
            nameFd.setEditable(true);
            updateBtn.setText("保存更改");
        }
    }

    public void BuyTheCommodity(ActionEvent actionEvent) {
        SystemViewController controller= (SystemViewController) ViewManager.CONTROLLER.get("SystemViewController");
        if(controller.SumLabel==null){
            controller.SumLabel=new Label();
        }
            Float oldSum = Float.parseFloat(controller.SumLabel.getText());
            Integer oldNumber = Integer.parseInt(surplus.getText());
            if (Addflag) {
                oldSum -= Integer.parseInt(numberField.getText()) * Float.parseFloat(priceFd.getText());
                oldNumber += Integer.parseInt(numberField.getText());

                //删出订单操作
                numberField.setText("0");

                SystemContext.getInstance().removeBuyList(id.getText());
                Addflag = false;
                addBtn.setText("加入订单");
            } else {
                //加入订单操作

                if (numberField.getText().equals(""))
                    numberField.setText("1");
                if(Integer.parseInt(numberField.getText())<=0){
                    numberField.setText("1");
                }

                Integer buyNumber = Integer.parseInt(numberField.getText());
                Integer num = Integer.parseInt(surplus.getText());
                if (num < buyNumber) {
                    buyNumber = num;
                    numberField.setText(buyNumber.toString());
                }

                Commodity commodity = new Commodity(Integer.parseInt(id.getText()), nameFd.getText(),
                        Float.parseFloat(priceFd.getText()),
                        Integer.parseInt(surplus.getText()), commitTA.getText(), Integer.parseInt(numberField.getText()));
                SystemContext.getInstance().addBuyList(commodity);

                oldSum += Integer.parseInt(numberField.getText()) * Float.parseFloat(priceFd.getText());
                oldNumber -= Integer.parseInt(numberField.getText());

                Addflag = true;
                addBtn.setText("删出订单");
            }

            //以保留2位小数的字符串更新总计
            DecimalFormat decimalFormat = new DecimalFormat(".00");
            controller.SumLabel.setText(decimalFormat.format(oldSum));
            surplus.setText(oldNumber.toString());
            StorageChange();
    }

    public void StorageChange(){
        Commodity commodity=new Commodity(Integer.parseInt(id.getText()),nameFd.getText(),Float.parseFloat(priceFd.getText()),Integer.parseInt(surplus.getText()),commitTA.getText());
        CommodityDaoImpl commodityDao=new CommodityDaoImpl();
        commodityDao.updateCommodity(commodity);
    }
}
